#!/usr/bin/env python3


#  Uruk Video Downloader (UVD)
#  
#  Copyright 2020 hayder majid <hayder@riseup.net>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#

from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
import subprocess
import os

gui=Tk()
gui.resizable(False, False)
gui.configure(background='#f5f6fa')
gui.title("Uruk Video Downloader")
gui.iconphoto(True, PhotoImage(file='/usr/share/pixmaps/uvd.png'))

def sMenu(e):
    w = e.widget
    menu.entryconfigure("Cut", command=lambda: w.event_generate("<<Cut>>"))
    menu.entryconfigure("Copy", command=lambda: w.event_generate("<<Copy>>"))
    menu.entryconfigure("Paste", command=lambda: w.event_generate("<<Paste>>"))
    menu.tk.call("tk_popup", menu, e.x_root, e.y_root)

def cMenu(w):
    global menu
    menu = Menu(w, tearoff=0)
    menu.add_command(label="Cut")
    menu.add_command(label="Copy")
    menu.add_command(label="Paste")
    
def fileRead():
    global outPath
    folderPath = os.environ['HOME']+'/.uvd'
    outDir = open(folderPath+'/outPath.txt', "r")
    outPath = str(outDir.readline())
    outDir.close()

def url_input():
    inputValue=textBox.get("1.0","end-1c")
    fileRead()
    os.system("xterm -e  aria2c -c -x 2 -d "+(outPath)+" "+(inputValue))
    messagebox.showinfo(title="Download Complete", message="Your download is complete!")


def eClear():
    textBox.delete('1.0', END)
    textBox.update()


labelWindow = Label(gui,text = "Uruk URL Grabber \n -------------------------------------------", fg = "#636e72", bg='#f5f6fa', font=('broadway', 14))
labelWindow.grid(column=1, row=0, padx=10, pady=10)

labelPlist = Label(gui,text = "Past Your\n URL Here -->", fg = "#636e72", bg='#f5f6fa')
labelPlist.grid(column=0, row=1, padx=1)

textBox=Text(gui, height=1, width=55, borderwidth=0, bg='#c8d6e5')
textBox.grid(column=1, row=1, padx=1)

buttonDls=Button(gui, height=2, width=13, text="Clear Entry", fg='white', bg='#5599ff', borderwidth=0, command=lambda: eClear())
buttonDls.grid(column=2, row=1, padx=2)

buttonD=Button(gui, height=2, width=13, text="Download", fg='white', bg='#5599ff', borderwidth=0, command=lambda: url_input())
buttonD.grid(column=1, row=2, padx=2)

cMenu(gui)
textBox.bind_class("Text", "<Button-3><ButtonRelease-3>", sMenu)
menubar = Menu(gui)

gui.geometry("690x230")

mainloop()
